
package server.console.groups.datamanage;


import client.MapleCharacter;
import com.alee.extended.button.WebSwitch;
import com.alee.extended.layout.TableLayout;
import com.alee.extended.panel.CenterPanel;
import com.alee.extended.panel.GroupPanel;
import com.alee.laf.button.WebButton;
import com.alee.laf.label.WebLabel;
import com.alee.laf.menu.WebMenuItem;
import com.alee.laf.menu.WebPopupMenu;
import com.alee.laf.optionpane.WebOptionPane;
import com.alee.laf.rootpane.WebDialog;
import com.alee.laf.rootpane.WebFrame;
import com.alee.laf.text.WebTextArea;
import com.alee.laf.text.WebTextField;
import constants.JobConstants;
import handling.world.WorldFindService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import server.MapleItemInformationProvider;
import server.console.Start;
import server.maps.MapleMap;
import server.maps.MapleMapFactory;
import tools.DateUtil;
import tools.MaplePacketCreator;
import tools.Pair;
import tools.StringUtil;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;









































 public class PlayerPane extends TabbedPane {
    /*  43 */   private static final Logger log = LogManager.getLogger();
       private static PlayerPane instance;
    /*  45 */   private final Map<Integer, Vector<String>> playerData = new TreeMap<>();
    /*  46 */   private final Map<Integer, Integer> ids = new TreeMap<>();
    /*  47 */   private final ReentrantReadWriteLock mutex = new ReentrantReadWriteLock();
    /*  48 */   private final WebTextArea processTextArea = new WebTextArea();
    /*  49 */   private final WebFrame processPane = new WebFrame()
             {


    };




    PlayerPane(WebFrame owner) {
        super(owner);
    }



    public static PlayerPane getInstance() {
        /*  61 */
        return getInstance((WebFrame) null);

    }



    public static PlayerPane getInstance(WebFrame owner) {
        /*  65 */
        if (instance == null) {
            /*  66 */
            instance = new PlayerPane(owner);

        }
        /*  68 */
        return instance;

    }



       void init() {
        /*  73 */
        this.idTableName.add("帐号属性");
        /*  74 */
        this.idTableName.add("值");

        /*  76 */
        this.dataTableName.add("角色ID");
        /*  77 */
        this.dataTableName.add("帐号");
        /*  78 */
        this.dataTableName.add("角色名");
        /*  79 */
        this.dataTableName.add("等级");
        /*  80 */
        this.dataTableName.add("职业");
        /*  81 */
        this.dataTableName.add("所在频道");
        /*  82 */
        this.dataTableName.add("所在地图");

        /*  84 */
        this.allDataTableName.add(new Pair(Boolean.valueOf(true), "角色ID"));
        /*  85 */
        this.allDataTableName.add(new Pair(Boolean.valueOf(true), "帐号"));
        /*  86 */
        this.allDataTableName.add(new Pair(Boolean.valueOf(true), "角色名"));
        /*  87 */
        this.allDataTableName.add(new Pair(Boolean.valueOf(true), "频道"));
        /*  88 */
        this.allDataTableName.add(new Pair(Boolean.valueOf(true), "地图"));
        /*  89 */
        this.allDataTableName.add(new Pair(Boolean.valueOf(true), "等级"));
        /*  90 */
        this.allDataTableName.add(new Pair(Boolean.valueOf(true), "职业"));
        /*  91 */
        this.allDataTableName.add(new Pair(Boolean.valueOf(true), "金币"));
        /*  92 */
        this.allDataTableName.add(new Pair(Boolean.valueOf(true), "RMB(元宝)"));
        /*  93 */
        this.allDataTableName.add(new Pair(Boolean.valueOf(true), "点券"));
        /*  94 */
        this.allDataTableName.add(new Pair(Boolean.valueOf(true), "抵用券"));

    }



    public void registerIDs(int playerid, MapleCharacter.PlayerObservable playerObservable) {
        /*  98 */
        this.mutex.writeLock().lock();

        try {
            /* 100 */
            this.ids.put(Integer.valueOf(playerid), Integer.valueOf(this.dataTable.getRowCount()));
            /* 101 */
            ((DefaultTableModel) this.dataTable.getModel()).addRow(new Vector());
            /* 102 */
            playerObservable.addObserver(new TableDataObserver());
            /* 103 */
            playerObservable.update();

        } finally {
            /* 105 */
            this.mutex.writeLock().unlock();

        }

    }



    public void removeIDs(int plyaerid, MapleCharacter.PlayerObservable playerObservable) {
        /* 110 */
        this.mutex.readLock().lock();

        try {
            /* 112 */
            if (this.ids.containsKey(Integer.valueOf(plyaerid))) {
                /* 113 */
                int index = ((Integer) this.ids.remove(Integer.valueOf(plyaerid))).intValue();
                /* 114 */
                ((DefaultTableModel) this.dataTable.getModel()).removeRow(index);
                /* 115 */
                playerObservable.deleteObservers();
                /* 116 */
                for (Map.Entry<Integer, Integer> entry : this.ids.entrySet()) {
                    /* 117 */
                    if (((Integer) entry.getValue()).intValue() > index) {
                        /* 118 */
                        entry.setValue(Integer.valueOf(((Integer) entry.getValue()).intValue() - 1));

                    }

                }

            }
            /* 122 */
        } catch (Exception e) {
            /* 123 */
            log.error("移除ID失败", e);

        } finally {
            /* 125 */
            this.mutex.readLock().unlock();

        }

    }



       String getTitle() {
        /* 131 */
        return "玩家角色";

    }




    protected DefaultTableModel getIDTableModel() {

        try {
            /* 137 */
            if (this.dataTable.getSelectedRow() == -1) {
                /* 138 */
                return super.getIDTableModel();

            }
            /* 140 */
            int playerid = Integer.parseInt(String.valueOf(this.dataTable.getValueAt(this.dataTable.getSelectedRow(), 0)));
            /* 141 */
            if (!this.playerData.containsKey(Integer.valueOf(playerid))) {
                /* 142 */
                return super.getIDTableModel();

            }
            /* 144 */
            MapleCharacter player = WorldFindService.getInstance().findCharacterById(playerid);
            /* 145 */
            if (player == null || player.getClient() == null) {
                /* 146 */
                return super.getIDTableModel();

            }
            /* 148 */
            Map<String, String> accInfoMap = player.getClient().getAccInfoFromDB();
            /* 149 */
            Map<String, String> dataMap = new LinkedHashMap<>();
            /* 150 */
            dataMap.put("帐号ID", String.valueOf(player.getAccountID()));
            /* 151 */
            dataMap.put("帐号名", player.getClient().getAccountName());
            /* 152 */
            dataMap.put("密码", accInfoMap.get("password"));
            /* 153 */
            dataMap.put("安全码", accInfoMap.get("safecode"));
            /* 154 */
            dataMap.put("交易码", accInfoMap.get("tradecode"));
            /* 155 */
            dataMap.put("创建时间", accInfoMap.get("createdat"));
            /* 156 */
            dataMap.put("上次登陆时间", accInfoMap.get("lastlogin"));
            /* 157 */
            dataMap.put("上次登陆IP", accInfoMap.get("sessionIP"));
            /* 158 */
            dataMap.put("生日", accInfoMap.get("birthday"));
            /* 159 */
            dataMap.put("QQ", accInfoMap.get("qq"));
            /* 160 */
            dataMap.put("邮箱", accInfoMap.get("email"));


            /* 163 */
            Vector<Vector<String>> datas = new Vector<>();
            /* 164 */
            for (Map.Entry<String, String> entry : dataMap.entrySet()) {
                /* 165 */
                Vector<String> data = new Vector<>();
                /* 166 */
                data.add(entry.getKey());
                /* 167 */
                data.add(entry.getValue());
                /* 168 */
                datas.add(data);

            }
            /* 170 */
            return new DefaultTableModel(datas, this.idTableName);
            /* 171 */
        } catch (Exception e) {
            /* 172 */
            log.error("", e);
            /* 173 */
            return new DefaultTableModel(null, this.idTableName);

        }

    }




    protected DefaultTableModel getDataTableModel() {

        try {
            /* 180 */
            this.dataTableName.clear();
            /* 181 */
            Vector<Vector<String>> datas = new Vector<>();

            /* 183 */
            for (Pair<Boolean, String> pair : this.allDataTableName) {
                /* 184 */
                if (((Boolean) pair.getLeft()).booleanValue()) {
                    /* 185 */
                    this.dataTableName.add(pair.getRight());

                }

            }

            /* 189 */
            int i = 0;
            /* 190 */
            for (Vector<String> vector : this.playerData.values()) {
                /* 191 */
                Vector<String> data = new Vector<>();

                /* 193 */
                for (String aVector : vector) {
                    /* 194 */
                    if (((Boolean) ((Pair) this.allDataTableName.get(i++)).getLeft()).booleanValue()) {
                        /* 195 */
                        data.add(aVector);

                    }

                }
                /* 198 */
                datas.add(data);

            }

            /* 201 */
            return new DefaultTableModel(datas, this.dataTableName);
            /* 202 */
        } catch (Exception e) {
            /* 203 */
            log.error("", e);
            /* 204 */
            return new DefaultTableModel(null, this.dataTableName);

        }

    }




    protected boolean showPopmenu() {
        /* 210 */
        return true;

    }




    protected boolean showOperation() {
        /* 215 */
        return false;

    }




    protected boolean showEditColumn() {
        /* 220 */
        return true;

    }




    protected void initColumnSizes() {
        /* 225 */
        for (int i = 0; i < this.dataTable.getColumnCount(); i++) {
            /* 226 */
            int width;
            TableColumn model = this.dataTable.getColumnModel().getColumn(i);

            /* 228 */
            switch (i) {

                case 0:
                    /* 230 */
                    width = 60;

                    break;

                case 3:

                case 5:
                    /* 234 */
                    width = 50;

                    break;

                case 4:
                    /* 237 */
                    width = 200;

                    break;

                default:
                    /* 240 */
                    width = 100;
                    break;

            }
            /* 242 */
            model.setPreferredWidth(width);

        }

    }




    protected WebPopupMenu getTablePopupMenu(boolean isIDTable) {
        /* 248 */
        WebPopupMenu popupMenu = new WebPopupMenu();

        /* 250 */
        if (!isIDTable) {


            /* 253 */
            popupMenu.add((JMenuItem) new PlayerWebMenuItem(new String[]{"踢下线"}, (player, o) -> {

                player.getClient().disconnect(true, false);

                if (player.getClient().getSession().isActive()) {

                    player.getClient().getSession().close();

                }

            }));
            /* 259 */
            popupMenu.add((JMenuItem) new PlayerWebMenuItem(new String[]{"封号", "封号理由", "是否封IP&MAC", "是否永久封号"}, (player, o) -> player.ban(o[0].toString(), Boolean.valueOf(o[1].toString()).booleanValue(), false, Boolean.valueOf(o[2].toString()).booleanValue())));
            /* 260 */
            popupMenu.add((JMenuItem) new PlayerWebMenuItem(new String[]{"传送", "地图ID"}, (player, o) -> {

                if (!checkNumber(o)) {

                    return;

                }


                MapleMap map = player.getClient().getChannelServer().getMapFactory().getMap(Integer.valueOf((String) o[0]).intValue());


                if (map == null) {

                    WebOptionPane.showMessageDialog((Component) instance, "输入的地图不存在");


                    return;

                }

                player.changeMap(map);

                player.dropMessage(1, "您被管理员传送到这里.");

            }));
            /* 275 */
            popupMenu.add((JMenuItem) new PlayerWebMenuItem(new String[]{"给予金币", "数量"}, (player, o) -> {

                if (!checkNumber(o)) {

                    return;

                }

                long meso = Long.valueOf((String) o[0]).longValue();

                player.gainMeso(meso, true);

                player.dropMessage(1, "您已获得管理员发放的" + meso + "金币");

            }));
            /* 283 */
            popupMenu.add((JMenuItem) new PlayerWebMenuItem(new String[]{"给予点券", "数量"}, (player, o) -> {

                if (!checkNumber(o)) {

                    return;

                }

                int cash = Integer.valueOf((String) o[0]).intValue();

                player.modifyCSPoints(1, cash, true);

                player.dropMessage(1, "您已获得管理员发放的 " + cash + " 点券");

            }));
            /* 291 */
            popupMenu.add((JMenuItem) new PlayerWebMenuItem(new String[]{"给予抵用券", "数量"}, (player, o) -> {

                if (!checkNumber(o)) {

                    return;

                }

                int cash = Integer.valueOf((String) o[0]).intValue();

                player.modifyCSPoints(2, cash, true);

                player.dropMessage(1, "您已获得管理员发放的 " + cash + " 抵用券");

            }));
            /* 299 */
            popupMenu.add((JMenuItem) new PlayerWebMenuItem(new String[]{"给予元宝(RMB)", "数量"}, (player, o) -> {

                if (!checkNumber(o)) {

                    return;

                }

                int rmb = Integer.valueOf((String) o[0]).intValue();

                player.gainRMB(rmb);

                player.dropMessage(1, "您已获得管理员发放的 " + rmb + " 元宝");

            }));
            /* 307 */
            popupMenu.add((JMenuItem) new PlayerWebMenuItem(new String[]{"给予元宝(HyPay)", "数量"}, (player, o) -> {

                if (!checkNumber(o)) {

                    return;

                }

                int rmb = Integer.valueOf((String) o[0]).intValue();

                player.gainHyPay(rmb);

                player.dropMessage(1, "您已获得管理员发放的 " + rmb + " 元宝");

            }));
            /* 315 */
            popupMenu.add((JMenuItem) new PlayerWebMenuItem(new String[]{"给予道具", "道具ID", "数量"}, (player, o) -> {

                if (!checkNumber(o)) {

                    return;

                }

                int itemID = Integer.valueOf((String) o[0]).intValue();

                short quantity = Short.valueOf((String) o[1]).shortValue();

                player.gainItem(itemID, quantity, "管理员在控制台赠送 时间:" + DateUtil.getNowTime());

                player.dropMessage(1, "您已获得管理员发放的 \r\n" + MapleItemInformationProvider.getInstance().getName(itemID) + " " + quantity + "个");

            }));
            /* 324 */
            popupMenu.add((JMenuItem) new PlayerWebMenuItem(new String[]{"查看进程"}, (player, o) -> {

                player.getClient().announce(MaplePacketCreator.SystemProcess());

                if (this.processPane.isVisible()) {

                    this.processPane.dispose();

                }

                this.processTextArea.clear();

                this.processPane.setTitle("查看进程 - " + player.getName());

                this.processPane.pack();

                this.processPane.setVisible(true);

            }));
            /* 334 */
            popupMenu.add((JMenuItem) new PlayerWebMenuItem(new String[]{"设置GM等级", "等级(0-6)"}, (player, o) -> {

                if (!checkNumber(o)) {

                    return;

                }

                Integer level = Integer.valueOf((String) o[0]);

                player.setGmLevel(level.intValue());

                player.dropMessage(1, "您的GM等级已更改为 " + level + " 级");

            }));
            /* 342 */
            popupMenu.add((JMenuItem) new PlayerWebMenuItem(new String[]{"封包调试窗口"}, (player, o) -> player.getClient().StartWindow()));

        }
        /* 344 */
        return popupMenu;

    }



    private boolean checkNumber(Object[] o) {
        /* 348 */
        for (Object o1 : o) {
            /* 349 */
            if (!StringUtil.isNumber(o1.toString())) {
                /* 350 */
                WebOptionPane.showMessageDialog((Component) instance, "输入的不是有效的数字");
                /* 351 */
                return false;

            }

        }
        /* 354 */
        return true;

    }



    public void printProcessList(String process) {
        /* 358 */
        if (process.endsWith("svchost.exe")) {

            return;

        }
        /* 361 */
        this.processTextArea.append(process + "\r\n");

    }



    @FunctionalInterface
     static interface PlayerAction {
             void run(MapleCharacter param1MapleCharacter, Object[] param1ArrayOfObject);

    }


       class PlayerWebMenuItem
             extends WebMenuItem {
        /* 371 */     private final Map<String, Object> values = new LinkedHashMap<>();
             private final PlayerPane.PlayerAction playerAction;



        public PlayerWebMenuItem(String[] text, PlayerPane.PlayerAction function) {
            /* 375 */
            super(text[0]);
            /* 376 */
            this.playerAction = function;
            /* 377 */
            addActionListener(e -> {

                if (PlayerPane.this.dataTable.getSelectedRowCount() <= 0) {

                    WebOptionPane.showMessageDialog((Component) PlayerPane.instance, "没有选择玩家,无法操作.", "警告", 0);

                    return;

                }

                if (text.length > 1) {

                    for (int i = 1; i < text.length; i++) {

                        String labelName = text[i];

                        if (labelName.contains("数量") || labelName.contains("ID")) {

                            this.values.put(labelName, Integer.valueOf(0));

                        } else if (labelName.contains("是否")) {

                            this.values.put(labelName, Boolean.valueOf(false));

                        } else {

                            this.values.put(labelName, "");

                        }

                    }

                    showInputDialog(text[0]);

                } else {

                    apply();

                }

            });

        }




        private void showInputDialog(String title) {
            /* 402 */
            final WebDialog dialog = new WebDialog((Frame) PlayerPane.this.owner, title, true);
            /* 403 */
            dialog.setResizable(false);
            /* 404 */
            GroupPanel groupPanel = new GroupPanel(5, false, new Component[0]);
            /* 405 */
            double[] doubles = new double[this.values.size() + 1];
            /* 406 */
            Arrays.fill(doubles, 0, doubles.length, -2.0D);
            /* 407 */
            groupPanel.setLayout((LayoutManager) new TableLayout(new double[]{-2.0D, -1.0D}, doubles, 5, 5));
            /* 408 */
            int i = 0;
            /* 409 */
            for (Map.Entry<String, Object> entry : this.values.entrySet()) {
                /* 410 */
                groupPanel.add((Component) new WebLabel(entry.getKey(), 11, new Object[0]), "0," + i);

                /* 412 */
                if (entry.getValue() instanceof Boolean) {
                    /* 413 */
                    WebSwitch ws2 = new WebSwitch(Boolean.valueOf(entry.getValue().toString()).booleanValue());
                    /* 414 */
                    ws2.setRound(11);
                    /* 415 */
                    ws2.setLeftComponent(PlayerPane.this.createSwitchIcon(Start.loadIcon("ok.png"), 4, 0));
                    /* 416 */
                    ws2.setRightComponent(PlayerPane.this.createSwitchIcon(Start.loadIcon("off.png"), 0, 4));
                    /* 417 */
                    ws2.addActionListener(e -> entry.setValue(Boolean.valueOf(ws2.isSelected())));
                    /* 418 */
                    groupPanel.add(new GroupPanel(ws2), "1," + i);

                } else {
                    groupPanel.add(new WebTextField(15) {
                        {
                            getDocument().addDocumentListener(new DocumentListener() {
                                @Override
                                public void insertUpdate(DocumentEvent e) {
                                    values.put(entry.getKey(), getText());
                                }

                                @Override
                                public void removeUpdate(DocumentEvent e) {
                                    values.put(entry.getKey(), getText());
                                }

                                @Override
                                public void changedUpdate(DocumentEvent e) {
                                    values.put(entry.getKey(), getText());
                                }
                            });
                            addActionListener(e -> {
                                apply();
                                dialog.dispose();
                            });
                        }
                    }, "1," + i);
                }
























                /* 446 */
                i++;

            }
            /* 448 */
            groupPanel.add((Component) new CenterPanel((Component) new GroupPanel(5, new Component[]{(Component) new WebButton("确定") {
            }, (Component) new WebButton("取消") {
            }})), "0," + this.values










/* 459 */.size() + ",1," + this.values.size());
            /* 460 */
            groupPanel.setMargin(10, 20, 10, 20);
            /* 461 */
            dialog.add((Component) new CenterPanel((Component) groupPanel));
            /* 462 */
            dialog.pack();
            /* 463 */
            dialog.setLocationRelativeTo((Component) PlayerPane.instance);
            /* 464 */
            dialog.setVisible(true);

        }



        private void apply() {
            /* 468 */
            List<Integer> selectedIDs = new ArrayList<>();
            /* 469 */
            for (int i : PlayerPane.this.dataTable.getSelectedRows()) {
                /* 470 */
                selectedIDs.add(Integer.valueOf(PlayerPane.this.dataTable.getModel().getValueAt(i, 0).toString()));

            }
            /* 472 */
            selectedIDs.parallelStream().forEach(playerID -> {

                final MapleCharacter player = WorldFindService.getInstance().findCharacterById(playerID.intValue());

                if (player != null) {

                    (new SwingWorker<Object, Object>()
                             {

                        protected Object doInBackground() {
                            /* 478 */
                            PlayerPane.PlayerWebMenuItem.this.playerAction.run(player, PlayerPane.PlayerWebMenuItem.this.values.values().toArray());
                            /* 479 */
                            return null;

                        }

                    }).execute();

                }

            });

        }

    }


       class TableDataObserver
             implements Observer
             {

        public void update(Observable o, Object arg) {

            try {
                /* 492 */
                MapleCharacter player = (MapleCharacter) arg;
                /* 493 */
                if (arg == null) {

                    return;

                }
                /* 496 */
                int row = ((Integer) PlayerPane.this.ids.get(Integer.valueOf(player.getId()))).intValue();
                /* 497 */
                Vector<String> alldata = new Vector<>(), viewdata = new Vector<>();

                /* 499 */
                alldata.add(String.valueOf(player.getId()));
                /* 500 */
                alldata.add(player.getClient().getAccountName());
                /* 501 */
                alldata.add(player.getName());
                /* 502 */
                alldata.add(String.valueOf(player.getClient().getChannel()));
                /* 503 */
                alldata.add(player.getMapId() + "(" + MapleMapFactory.getMapName(player.getMapId()) + ")");
                /* 504 */
                alldata.add(String.valueOf(player.getLevel()));
                /* 505 */
                alldata.add(player.getJob() + "(" + JobConstants.getJobNameById(player.getJob()) + ")");
                /* 506 */
                alldata.add(String.valueOf(player.getMeso()));
                /* 507 */
                alldata.add(String.valueOf(player.getRMB()));
                /* 508 */
                alldata.add(String.valueOf(player.getCSPoints(1)));
                /* 509 */
                alldata.add(String.valueOf(player.getCSPoints(2)));

                /* 511 */
                PlayerPane.this.playerData.put(Integer.valueOf(player.getId()), alldata);


                int i;
                /* 514 */
                for (i = 0; i < PlayerPane.this.allDataTableName.size(); i++) {
                    /* 515 */
                    Pair<Boolean, String> pair = PlayerPane.this.allDataTableName.get(i);
                    /* 516 */
                    if (((Boolean) pair.getLeft()).booleanValue()) {
                        /* 517 */
                        viewdata.add(alldata.get(i));

                    }

                }

                /* 521 */
                for (i = 0; i < viewdata.size(); i++) {
                    /* 522 */
                    PlayerPane.this.dataTable.setValueAt(viewdata.get(i), row, i);

                }
                /* 524 */
            } catch (Exception e) {
                /* 525 */
                PlayerPane.log.error("更新角色数据表失败", e);

            }

        }

    }

}


/* Location:              C:\PGX\Server.jar!\server\console\groups\datamanage\PlayerPane.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */